﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Silverlight.Validators.Controls
{
    public partial class DefaultIndicator : UserControl, IIndicator
    {
        private ValidatorBase _Validator;
        private bool _InGrid = false;

		public DefaultIndicator()
        {
            InitializeComponent();

            _Border.Width = this.Width;
			_Border.Height = this.Height;
            _TextBlock.FontSize = this.Height * .8;


			this.Loaded += new RoutedEventHandler(Indicator1_Loaded);

            HideIndicatorStory.Completed += delegate(System.Object o, System.EventArgs e)
            {
                this.Visibility = Visibility.Collapsed;
            };
        }

        void Indicator1_Loaded(object sender, RoutedEventArgs e)
        {
            this.UpdateLayout();
            this.ApplyTemplate();
        }

        public void ShowIndicator(ValidatorBase validator)
        {
			try
			{
               // validator.ElementToValidate.LayoutUpdated += new EventHandler(Panel_LayoutUpdated);
                _Validator = validator;

				if (!string.IsNullOrEmpty(validator.ErrorMessage))
				{
					ToolTipService.SetToolTip(this, validator.ErrorMessage);
				}
			
				if (this.Parent == null)
				{
                    if (validator.Panel is Grid)
                    {
                        _InGrid = true;
                        int col = Grid.GetColumn(validator.ElementToValidate);
                        int row = Grid.GetRow(validator.ElementToValidate);

                        Grid.SetRow(this, row);
                        Grid.SetColumn(this, col);
                    }
					//Panel g = (Panel)validator.UserControl.FindName("LayoutRoot");
                    validator.Panel.Children.Add(this);
                    //validator.Panel.LayoutUpdated += new EventHandler(Panel_LayoutUpdated);
				}
                PositionIndicator(validator);

				HideIndicatorStory.Stop();
				ShowIndicatorStory.Begin();
			}
			catch(Exception e) {
				var s = e;
			}
            this.Visibility = Visibility.Visible;

        }
        /*
        void Panel_LayoutUpdated(object sender, EventArgs e)
        {
            PositionIndicator(_Validator);
        }
        */

        public void HideIndicator()
		{
            ShowIndicatorStory.Stop();
            HideIndicatorStory.Begin();

		}
        private void PositionIndicator(ValidatorBase validator)
        {
            if (_InGrid)
            {
                //double controlTop = (validator.ElementToValidate.ActualHeight / 2) - (this.Height / 2);

                double controlTop = 0;

                if (validator.ElementToValidate.Margin.Top > 0)
                {
                    controlTop = validator.ElementToValidate.Margin.Top;// +(this.Height / 4);
                }

                double controlLeft = validator.ElementToValidate.Margin.Left + validator.ElementToValidate.ActualWidth + 3;

                this.VerticalAlignment = validator.ElementToValidate.VerticalAlignment;
                this.Margin = new Thickness(controlLeft, controlTop, 0, 0);
                
            }
            else
            {
                PositionIndicatorNotInGrid(validator);
            }
        }

        private void PositionIndicatorNotInGrid(ValidatorBase validator)
        {
            GeneralTransform gt = null;
            try
            {
                gt = validator.ElementToValidate.TransformToVisual(validator.Panel);
            }
            catch (ArgumentException ex)
            {
                //will sometimes throw an argumentexception when loading or unloading a control.
                this.Visibility = Visibility.Collapsed;
                return;
            }

            Point offset = gt.Transform(new Point(0, 0));

            //Debug.WriteLine("Y: " + offset.Y.ToString() + ", X: " + offset.X);
            double controlTop = offset.Y + (validator.ElementToValidate.ActualHeight / 2) - (this.Height / 2);
            double controlLeft = offset.X + validator.ElementToValidate.ActualWidth + 3;

            //offset.Y >= 0 &&
            if (offset.Y > 0 && offset.Y < (validator.Panel.ActualHeight - validator.ElementToValidate.ActualHeight))
            {
                this.Visibility = Visibility.Visible;
                this.Margin = new Thickness(controlLeft, controlTop, 0, 0);
            }
            else
            {
                this.Visibility = Visibility.Collapsed;
            }
        }
    }
}
